package com.raymond.redis;

import com.alibaba.fastjson.JSONObject;
import com.raymond.bean.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;

import java.util.List;

/**
 * redis消费者
 *
 * @author :  raymond
 * @version :  1.0
 * @date :  2019/7/17
 */
@Component
@Slf4j
public class RedisConsumer {

    private final JedisCluster redisCluster;

    public RedisConsumer(JedisCluster redisCluster) {
        this.redisCluster = redisCluster;
    }

    public List<String> consumers(int timeout, String key) {
        return redisCluster.brpop(timeout, key);
    }

    public Message getMessage(int timeout, String key){
        List<byte[]> messages = redisCluster.brpop(timeout, key.getBytes());
        if (messages != null && messages.size() > 0) {
            return JSONObject.parseObject(messages.get(1), Message.class);
        }
        return null;
    }
}
